<?php

/**
 * 魔法召唤
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class MagicController extends AdminbaseController
{
	// 配置类型
	protected function getTypes($k = '')
	{
		$type = ['0' => '无奖', '1' => '钻石', '2' => '礼物', '3' => '线下奖品',];
		$type = ['common' => '普通', 'high' => '高级', 'luxury' => '豪华'];
		if($k === ''){
			return $type;
		}
		
		return $type[$k] ?? '';
	}
	
	// 奖品列表
	function index()
	{
		$all   = $this->getTypes();
		$lists = Db::name("magic")->order("id asc")->paginate(20);
		$lists->each(function($v, $k) use ($all) {
			$giftinfo = Db::name("gift")->field('giftname')->where("id={$v['giftid']}")->find();
			$types    = explode(',', $v['types']);
			
			//$v['needcoin'] = $v['needcoin'] ?: ($giftinfo['needcoin'] ?? '0');
			$v['name']  = $giftinfo['giftname'] ?? '-';
			$v['thumb'] = get_upload_path($v['thumb']);
			$v['types'] = '';
			foreach($types as $_type){
				$v['types'] .= $all[$_type].'  ';
			}
			
			return $v;
		});
		
		$page = $lists->render();
		
		//$this->assign('types', $this->getTypes());
		$this->assign('lists', $lists);
		$this->assign("page", $page);
		
		
		return $this->fetch();
	}
	
	// 修改 详情展示
	function edit()
	{
		$id   = $this->request->param('id', 0, 'intval');
		$data = Db::name('magic')->where("id={$id}")->find();
		if(!$data){
			$this->error("信息错误");
		}
		
		/* $gift=Db::name('gift')->field('id,giftname')->where([ ['type','<>',2] ])->order('list_order desc')->select(); */
		$gift = Db::name('gift')->field('id,giftname,needcoin')->where('type not in (2,3)')->order('list_order desc')->select();
		
		// 数据整理
		// 类型：多个值
		$data['types'] = explode(',', $data['types']);
		/*if(empty($data['needcoin'])){
			foreach($gift as $_gift){
				if($_gift['id'] != $data['giftid']){
					continue;
				}
				$data['needcoin'] = $_gift['needcoin'];
			}
		}*/
		
		$this->assign('gift', $gift);
		$this->assign('data', $data);
		$this->assign('allType', $this->getTypes());
		
		return $this->fetch();
	}
	
	// 修改
	function editPost()
	{
		if($this->request->isPost()){
			$data   = $this->request->param();
			$giftid = intval($data['giftid']);
			if($giftid < 1){
				$this->error('请输入选择礼物');
			}
			
			$data['uptime'] = time();
			$data['types']  = implode(',', $data['types']);
			unset($data['coin'], $data['name'], $data['thumb_old']);
			$rs = DB::name('magic')->update($data);
			if($rs === false){
				$this->error("修改失败！");
			}
			
			$action = "修改魔法召唤奖品列表ID: ".$data['id'];
			setAdminLog($action);
			$this->resetcache();
			$this->success("修改成功！");
		}
	}
	
	// 重新缓存
	function resetcache()
	{
		$key  = 'magic';
		$list = Db::name('magic')->select();
		if($list){
			setcaches($key, $list);
		} else{
			delcache($key);
		}
		
		return 1;
	}
	
	// 日志记录
	function index2()
	{
		$data = $this->request->param();
		$map  = [];
		
		$start_time = $data['start_time'] ?? '';
		$end_time   = $data['end_time'] ?? '';
		if($start_time != ""){
			$map[] = ['addtime', '>=', strtotime($start_time)];
		}
		if($end_time != ""){
			$map[] = ['addtime', '<=', strtotime($end_time) + 60 * 60 * 24];
		}
		
		$uid = $data['uid'] ?? '';
		if($uid != ''){
			$lianguid = getLianguser($uid);
			if($lianguid){
				//$map[]=['uid',['=',$uid],['in',$lianguid],'or'];
				array_push($lianguid, $uid);
				$map[] = ['uid', 'in', $lianguid];
			} else{
				$map[] = ['uid', '=', $uid];
			}
		}
		
		$liveuid = $data['liveuid'] ?? '';
		if($liveuid != ''){
			$lianguid = getLianguser($liveuid);
			if($lianguid){
				//$map[]=['liveuid',['=',$liveuid],['in',$lianguid],'or'];
				array_push($lianguid, $liveuid);
				$map[] = ['liveuid', 'in', $lianguid];
			} else{
				$map[] = ['liveuid', '=', $liveuid];
			}
		}
		
		$showid = $data['showid'] ?? '';
		if($showid != ''){
			$map[] = ['showid', '=', $showid];
		}
		
		$lists = Db::name("magic_log")->where($map)->order("id DESC")->paginate(20);
		$lists->each(function($v, $k) {
			$userinfo         = getUserInfo($v['uid']);
			$v['userinfo']    = $userinfo;
			$liveuidinfo      = getUserInfo($v['liveuid']);
			$v['liveuidinfo'] = $liveuidinfo;
			
			$winlist = [];
			if($v['iswin'] == 1){
				$winlist = Db::name("magic_win")->where("logid={$v['id']}")->select();
				
				foreach($winlist as $k2 => $v2){
					$name     = '已删除';
					$giftinfo = Db::name("gift")->field('giftname')->where("id={$v2['giftid']}")->find();
					if($giftinfo){
						$name = $giftinfo['giftname'];
					}
					
					
					$v2['name']   = $name;
					$winlist[$k2] = $v2;
				}
			}
			
			$v['winlist'] = $winlist;
			
			return $v;
		});
		
		$lists->appends($data);
		$page = $lists->render();
		
		$this->assign('lists', $lists);
		$this->assign("page", $page);
		$this->assign('types', $this->getTypes());
		
		$count = Db::name("magic_log")->where($map)->count();
		$total = Db::name("magic_log")->where($map)->sum('coin');
		$ticks = Db::name("magic_log")->where($map)->sum('ticket');
		if(!$total){
			$total = 0;
		}
		if(!$ticks){
			$ticks = 0;
		}
		
		$this->assign("count", $count);
		$this->assign("total", $total);
		$this->assign("ticks", $ticks);
		
		return $this->fetch();
	}
	
	// 线下奖品
	function index3()
	{
		$data = $this->request->param();
		$map  = [];
		
		$status = $data['status'] ?? '';
		if($status != ''){
			$map[] = ['status', '=', $status];
		}
		
		$uid = $data['uid'] ?? '';
		if($uid != ''){
			$lianguid = getLianguser($uid);
			if($lianguid){
				//$map[]=['uid',['=',$uid],['in',$lianguid],'or'];
				array_push($lianguid, $uid);
				$map[] = ['uid', 'in', $lianguid];
			} else{
				$map[] = ['uid', '=', $uid];
			}
		}
		
		$lists = Db::name("magic_win")->where($map)->order("id desc")->paginate(20);
		$lists->each(function($v, $k) {
			$userinfo = getUserInfo($v['uid']);
			$giftname = '';
			$giftinfo = Db::name("gift")->field('giftname')->where("id={$v['giftid']}")->find();
			if($giftinfo){
				$giftname = $giftinfo['giftname'];
			}
			
			$v['giftname'] = $giftname;
			$v['userinfo'] = $userinfo;
			
			return $v;
		});
		
		$page = $lists->render();
		
		$this->assign('lists', $lists);
		$this->assign("page", $page);
		$this->assign('types', $this->getTypes());
		
		return $this->fetch();
	}
	
	// 线下奖品的状态设置
	function setStatus()
	{
		$id     = $this->request->param('id', 0, 'intval');
		$status = $this->request->param('status', 0, 'intval');
		$rs     = DB::name('magic_win')->where("id={$id}")->update(['status' => $status, 'uptime' => time()]);
		if(!$rs){
			$this->error("操作失败！");
		}
		$action = "标记处理奖品列表ID: ".$id;
		setAdminLog($action);
		
		$this->success("操作成功！");
	}
	
}
